package com.yiren.backstage.judge.api.impl;

import android.content.Context;

import com.yiren.backstage.currency.constant.AutoJudgeConstant;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.currency.utils.MessageUtil;
import com.yiren.backstage.db.dao.cache.ProjectCache;
import com.yiren.backstage.judge.api.IExamJudge;
import com.yiren.backstage.judge.api.IExamStateCallBack;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.handler.LightJudgeHandler;
import com.yiren.backstage.judge.thread.AutoPlayThread;
import com.yiren.backstage.judge.thread.CachedThreadPoolManage;
import com.yiren.backstage.player.api.PlayerApi;

import org.apache.commons.collections.map.HashedMap;

import java.util.Map;

/**
 * Time:2021/11/08 16:17
 * Author:xiaozan
 * Description:开始考试 接口实现类
 */
public class ExamJudgeApiImpl implements IExamJudge {

    private Context context;

    private AutoJudgeHandler autoJudgeHandler;

    private LightJudgeHandler lightJudgeHandler;

    private IExamStateCallBack iExamStateCallBack;

    /**项目执行状态*/
    public static Map<String,ProjectEntity> projectStateMap = new HashedMap();

    public ExamJudgeApiImpl(Context context, IExamStateCallBack iExamStateCallBack) {
        this.context = context;
        this.iExamStateCallBack = iExamStateCallBack;
        autoJudgeHandler = new AutoJudgeHandler(context, iExamStateCallBack);
        lightJudgeHandler = new LightJudgeHandler(context, iExamStateCallBack);
    }

    @Override
    public void startExam(ProjectEntity projectEntity, LineInfoEntity lineInfoEntity) {
        //开始前关闭语音播报
        PlayerApi.resetAllPlayer();
        autoJudgeHandler.setTaskStateEnum(TaskStateEnum.IN_PROCESS);
        lightJudgeHandler.setTaskStateEnum(TaskStateEnum.IN_PROCESS);
        //启动开始考试时第一个执行的项目
        startProjectJudge(projectEntity);
        //启动全程评判项目
        autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.START_WHOLE_COURSE_PROJECT_JUDGE, ProjectCache.subjectThreeFullJudge));
        //启动自动播报线程
        CachedThreadPoolManage.getInstance().startThread(new AutoPlayThread(context, autoJudgeHandler, lineInfoEntity, iExamStateCallBack));
    }

    @Override
    public void endExam() {
        //关闭当前正在播报的语音
        PlayerApi.resetAllPlayer();
        autoJudgeHandler.setTaskStateEnum(TaskStateEnum.FORCT_CLOSE);
        lightJudgeHandler.setTaskStateEnum(TaskStateEnum.FORCT_CLOSE);
    }

    @Override
    public void exitExam() {
        //释放播报器
        PlayerApi.releaseAllPlayer();
        autoJudgeHandler.setTaskStateEnum(TaskStateEnum.FORCT_CLOSE);
        lightJudgeHandler.setTaskStateEnum(TaskStateEnum.FORCT_CLOSE);
    }

    @Override
    public void startProjectJudge(ProjectEntity projectEntity) {
        if (null == projectEntity) return;

        //将项目执行状态改成正在执行
        iExamStateCallBack.projectState(projectEntity, TaskStateEnum.IN_PROCESS);

        projectEntity.setTaskState(TaskStateEnum.IN_PROCESS);
        projectStateMap.put(projectEntity.getId(),projectEntity);

        switch (ProjectTypeEnum.valueOf(projectEntity.getProjectType())) {
            case SUBJECT_THREE_DAYTIME:
                autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_START_VOICE, projectEntity));
                break;
            case LIGHT:
                lightJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.START_UP_LIGHT_TIPS, projectEntity));
                break;
        }
    }
}
